package com.jse.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpCookie;
import java.util.Map;
import com.jse.Http;
import com.jse.Lang;
import com.jse.json.JsonObject;

public interface Ctx extends Map<String,Object> {

	public Object req();
	public Object res();
	public String getPath();
	public String getServletPath();
	public String getQueryString();
	public String getMapping();
	public String getSuffix();
	public String getMethod();
	public String getServerName();
	public JsonObject tbl();
	public void setHeader(String name,String value);
	public String getHeader(String name);
	public void setAttribute(String name,Object value);
	public Object getAttribute(String name);
	public void sattr(String name,Object value);
	public Object sattr(String name);
	public Object getSession();
	public Map<String,Object> sessionMap();
	public String getRequestedSessionId();
	public Map<String,HttpCookie> cookies();
	default HttpCookie cookie(String name) {return cookies().get(name);}
	public void addCookie(HttpCookie cookie);
	default void addCookie(Map<String,Object> hcookie) {
		HttpCookie cookie=new HttpCookie(hcookie.get("name").toString(),hcookie.get("value").toString());
		if(Boolean.TRUE==hcookie.get("secure"))cookie.setSecure(true);
		if(hcookie.get("domain")!=null)cookie.setDomain(hcookie.get("domain").toString());
		if(hcookie.get("maxAge")!=null)cookie.setMaxAge((int)hcookie.get("maxAge"));
		if(hcookie.get("path")!=null)cookie.setPath(hcookie.get("path").toString());
		if(Boolean.TRUE==hcookie.get("httpOnly"))cookie.setHttpOnly(true);
		addCookie(cookie);
	}
	default void delCookie() {
		Map<String,HttpCookie> cookies = cookies();
		for (String n:cookies.keySet()) {
			HttpCookie c=cookies.get(n);
		        if (c.getName().equals(n)||n==null){
		        	HttpCookie cookie = new HttpCookie(c.getName(),null);//cookie名字要相同
		        	cookie.setMaxAge(0); //
		            cookie.setPath("/");  // 相同路径
		            addCookie(cookie);
		        }
		}
		cookies.clear();
	}
	default void delCookie(String name) {
		HttpCookie cookie = new HttpCookie(name,null);//cookie名字要相同
    	cookie.setMaxAge(0); //
        cookie.setPath("/");  // 相同路径
        addCookie(cookie);
        cookies().remove(name);
	}
	public String reqcontentType();
	public String getContentType();
	public void setContentType(String type);
	public void setStatus(int status);
	public int httpStatus();
	default void sendError(int status) {sendError(status,Http.msg(status));}
	public void sendError(int status,String msg);
	public InputStream getRequestBody();
	public OutputStream getResponseBody();
	public boolean isCommitted();
	public Map<String, Object> attr();
	default void sendResponseHeaders(long contentLen)throws IOException{sendResponseHeaders(httpStatus(),contentLen);}
	public void sendResponseHeaders(int code,long len)throws IOException;
	public String getRemoteAddr();
	default boolean isMobile() {return Lang.isMobile(getHeader("user-agent"));}
	default boolean isAndroid() {return Lang.isAndroid(getHeader("user-agent"));}
	default boolean isIos() {return Lang.isIOS(getHeader("user-agent"));}
	default boolean isWeChat(){return Lang.isWeChat(getHeader("user-agent"));}
	void sattr();
}
